<template>
  <div class="org-modify-wrap">
    <el-dialog :visible.sync="dialogVisible_modify.show" center>
      <div class="show-title">分组ID：{{ org_id }}</div>
      <!-- <div class="show-title">上上级分组名称{{ gfather_name }}</div>
      <div class="show-title">上级分组名称{{ father_name }}</div>
      <div class="show-title">分组名称：{{ org_name_check }}</div> -->
      <div class="center">
        <el-form :inline="true" :model="ruleform" ref="form" :rules="rules" label-width="160px" label-position="right">
          <el-form-item label="上上级分组名称" prop="gfather_name">
            <el-input v-model="ruleform.gfather_name" placeholder style="width:280px"></el-input>
          </el-form-item>
          <br>
          <el-form-item label="上级分组名称" prop="father_name"  label-width="160px" label-position="right">
            <el-input v-model="ruleform.father_name" placeholder style="width:280px"></el-input>
          </el-form-item>
          <br>
          <el-form-item label="分组名称" prop="org_name"  label-width="160px" label-position="right">
            <el-input v-model="ruleform.org_name" placeholder style="width:280px"></el-input>
          </el-form-item>
          <br />
          <el-form-item class="btn-right">
            <el-button type="primary" size="small" @click="submitModify('form')"
              >修改</el-button
            >
            <el-button
              type="warning"
              size="small"
              plain
              @click="onReset('form')"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";

export default {
  name: "audit",
  props: {
    org_id: {
      type: String,
      default: "",
    },
    gfather_name: {
      type: String,
      default: "",
    },
    father_name: {
      type: String,
      default: "",
    },
    org_name: {
      type: String,
      default: "",
    },
    dialogVisible_modify: {
      type: Object,
      default() {
        return {
          show: false,
        };
      },
    },
  },
  data() {
    return {
      org_id_check: "",
      org_name_check: "",
      ruleform: {
        gfather_name: "",
        father_name: "",
        org_name: "",
      },
      rules: {
        gfather_name: [{ required: true, message: "请输入上上级分组名称", trigger: "blur" }],
        father_name: [{ required: true, message: "请输入上级分组名称", trigger: "blur" }],
        org_name: [{ required: true, message: "请输入分组名称", trigger: "blur" }],
      },
    };
  },
  watch: {
    org_id: {
      handler(newVal) {
        this.org_id_check = newVal;
      },
      immediate: true,
    },
    gfather_name: {
      handler(newVal) {
        this.ruleform.gfather_name = newVal;
      },
      immediate: true,
    },
    father_name: {
      handler(newVal) {
        this.ruleform.father_name = newVal;
      },
      immediate: true,
    },
    org_name: {
      handler(newVal) {
        this.ruleform.org_name = newVal;
      },
      immediate: true,
    },
  },
  methods: {
    //修改 提交
    submitModify(formName) {
      var that = this;
      that.$refs[formName].validate((valid) => {
          if (
          that.ruleform.gfather_name == "" ||
          that.ruleform.father_name == "" ||
          that.ruleform.org_name == ""
        ) {
        //   this.$message.error("请先输入必填项！");
          return;
        }
        if (
          that.ruleform.gfather_name.length>20 ||
          that.ruleform.father_name.length>20 ||
          that.ruleform.org_name.length>20 
        ) {
           this.$message.error("分组名称须1-20个字符");
          return;
        }
        if (valid) {
          postData("/adminweb/org/update", {
            org_id: this.org_id_check,
            gfather_name: that.ruleform.gfather_name,
            father_name: that.ruleform.father_name,
            org_name: that.ruleform.org_name,
          })
            .then((res) => {
              if (res.code == 0) {
                that.$message.success("修改成功！");
                that.org_name_check = that.ruleform.org_name;
                that.$emit("refreshTableListModify");
              } else {
                that.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res.code + "，" + res.msg);
            });
        } else {
          that.$message.error("修改失败");
          return false;
        }
      });
    },
    // 重置
    onReset(val) {
      this.ruleform.gfather_name=""
      this.ruleform.father_name=""
      this.ruleform.org_name=""
    },
  },
};
</script>

<style scoped>
.center {
  margin-top: 30px;
  width: 100%;
  height: 400px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.show-title {
  height: 30px;
  line-height: 30px;
  font-weight: 600;
  color: #676a6d;
}

.btn-right{
    float: right;
}
.org-modify-wrap >>> .el-dialog {
  width: 30%;
}
.org-modify-wrap >>> .el-dialog--center .el-dialog__body{
    padding: 20px !important;
    height: 340px!important;
}

.org-modify-wrap >>> .el-dialog{
    margin-top:25vh!important;
}
</style>
